package com.zhanglu.authentication.service;

import com.zhanglu.authentication.entity.AuthPermission;
import com.zhanglu.authentication.mapper.AuthPermissionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @description
 * @Auther zhanglu
 * @Date 2017/12/6 下午2:52
 */
@Service
@Slf4j
public class PermissionService {
    @Autowired
    private AuthPermissionMapper permissionMapper;

    public List<AuthPermission> getPermissionAll() {
        List<AuthPermission> authPermissions = permissionMapper.selectAll();
        return Objects.isNull(authPermissions) ? new ArrayList<>() : authPermissions;
    }

    public List<AuthPermission> getPermissions(AuthPermission authPermission) {
        List<AuthPermission> authPermissions = permissionMapper.select(authPermission);
        return Objects.isNull(authPermissions) ? new ArrayList<>() : authPermissions;
    }

    public List<String> getRoleByServerNameAndUrlAndMethodType(String serverName, String url, String methodType) {
        List<String> roles = permissionMapper.getRoleByServerNameAndUrlAndMethodType(serverName, url, methodType);
        if (Objects.isNull(roles)) {
            roles = new ArrayList<>();
        }
        log.info("getRoleByServerNameAndUrlAndMethodType get roles are {}", roles);
        return roles;
    }
}
